"""
allure 常用方法抽离，截图方法实现
"""
import functools
import os.path

import allure
import pytest

from Config.Config import Config


class PrettyAllure(object):

    @classmethod
    def PrettyAllureCase(cls, CaseData):
        # epic描述 敏捷里面的概念
        # allure.dynamic.epic(CaseData.get("模块"))

        # 模块名称 功能点的描述
        allure.dynamic.feature(CaseData.get("模块"))
        # 模块描述
        allure.dynamic.description(CaseData.get('描述'))
        # 用户故事
        allure.dynamic.story(CaseData.get("功能"))
        # 用例的标题
        allure.dynamic.title(f'{CaseData.get("用例编号")}_{CaseData.get("用例标题")}')
        # 用例的的等级
        allure.dynamic.severity(CaseData.get("优先级"))
        # 用例的的地址
        allure.dynamic.link(CaseData.get("url地址"))

        if CaseData.get("是否执行") != "Y":
            allure.dynamic.description("用例指定跳过")
            pytest.skip("用例指定跳过")

    @classmethod
    def PrettyAllureScreenShot(cls, page, CaseData):
        file_path = os.path.join(Config.test_screenshot_dir, f"{CaseData.get('用例标题')}.png")
        page.screenshot(path=file_path)
        allure.attach.file(source=file_path, name=CaseData.get('用例标题'), attachment_type=allure.attachment_type.PNG)

    @classmethod
    def PrettyAllureWarpper(cls, func):
        """装饰器函数"""

        @functools.wraps(func)
        def inner(*args, **kwargs):
            # 添加用例信息
            # page = kwargs.get("page"),
            cls.PrettyAllureCase(CaseData=kwargs.get("CaseData"))
            r = func(*args, **kwargs)  # 运行用例
            # 添加截图
            cls.PrettyAllureScreenShot(page=kwargs.get("page"), CaseData=kwargs.get("CaseData"))
            return r

        return inner
